package com.lwj.liwanjia.view.activity.shop.rihuaxihu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.MainActivity;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.LeftListAdapter;
import com.lwj.liwanjia.adapter.RightAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.LeftBean;
import com.lwj.liwanjia.bean.RightBean;
import com.lwj.liwanjia.impl.JuliView;
import com.lwj.liwanjia.impl.LeftView;
import com.lwj.liwanjia.impl.RightView;
import com.lwj.liwanjia.presenter.LeftPresenter;
import com.lwj.liwanjia.presenter.RightJlPResenter;
import com.lwj.liwanjia.presenter.RightPresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.search.SearchActivity;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


public class RhxhTypeActivity extends BaseActivity implements LeftView, RightView, JuliView {

    @BindView(R.id.gouwuche_number)
    TextView gouwucheNumber;
    @BindView(R.id.left_listview)
    ListView leftListview;
    @BindView(R.id.tv_paiming)
    TextView tvPaiming;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.iv_gao_price)
    ImageView ivGaoPrice;
    @BindView(R.id.iv_lower_price)
    ImageView ivLowerPrice;
    @BindView(R.id.tv_pingfen)
    TextView tvPingfen;
    @BindView(R.id.iv_goa_pingfen)
    ImageView ivGoaPingfen;
    @BindView(R.id.iv_lower_pingfen)
    ImageView ivLowerPingfen;
    @BindView(R.id.tv_juli)
    TextView tvJuli;
    @BindView(R.id.iv_gao_juli)
    ImageView ivGaoJuli;
    @BindView(R.id.iv_lower_juli)
    ImageView ivLowerJuli;
    @BindView(R.id.rlv_rhxh)
    SuperRecyclerView rlvRhxh;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;

    private String token;
    private SpfUtils spfUtils;
    private LeftListAdapter leftListAdapter;
    private List<String> leftList = new ArrayList<>();//左侧选项
    private List<Boolean> flagArray = new ArrayList<>();//左侧选中
    private LeftPresenter leftPresenter;
    private int msg_type_id;
    private List<LeftBean.DataBean> data;
    private RightPresenter rightPresenter;
    private Map<String, Object> map = new HashMap<>();//除了距离的集合
    private Map<String, Object> map1 = new HashMap<>();//距离排序的集合
    private RightAdapter rightAdapter;
    private List<RightBean.DataBean> rightList = new ArrayList<>();
    private String tvRight = "综合排名";//右边排序类型
    private int desc = 0;//顺序
    private int tvLeft;//左边类型
    private TextView tvType;//右边textView
    private int isFirst0 = 0;//是否是第一次 价格
    private int isFirst1 = 0;//是否是第一次  评分
    private int isFirst2 = 0;//是否是第一次  距离
    private ImageView ivGao, ivDi;
    private RightJlPResenter jLpresenter;

    @Override
    protected void initView() {
        msg_type_id = getIntent().getIntExtra("msg_type_id", 0);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rlvRhxh.setLayoutManager(manager);
        rlvRhxh.setRefreshEnabled(false);
        rlvRhxh.setLoadMoreEnabled(false);
        rightAdapter = new RightAdapter(this, rightList);
        rlvRhxh.setAdapter(rightAdapter);
        rightPresenter = new RightPresenter(this);
        jLpresenter = new RightJlPResenter(this);

        spfUtils = SpfUtils.getSpfUtils(this);
        gouwucheNumber.setText(spfUtils.getGwcNumber() + "");
        leftListAdapter = new LeftListAdapter(this, leftList, flagArray);
        View rightHeader = LayoutInflater.from(this).inflate(R.layout.left_header_item, null);
        leftListview.addHeaderView(rightHeader, null, false);
        leftListview.setAdapter(leftListAdapter);
        leftPresenter = new LeftPresenter(this);
        token = SpfUtils.getSpfUtils(this).getToken();

        tvType = tvPaiming;
        ivGao = ivGaoPrice;
        ivDi = ivLowerPrice;

        map.put("token", token);
        map.put("sortType", tvRight);
        map.put("desc", desc);
        listener();
    }

    private void listener() {
        leftListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < leftList.size(); i++) {
                    if (i == position - 1) {
                        flagArray.add(i, true);
                    } else {
                        flagArray.add(i, false);
                    }
                }
                tvLeft = data.get(position - 1).getTypeId();
                if (!tvRight.equals("距离远近")) {
                    map.put("sortType", tvRight);
                    map.put("typeId", tvLeft);
                    map.put("desc", desc);
                    rightPresenter.getRightData(map);
                    leftListAdapter.notifyDataSetChanged();
                } else {
                    map1.put("typeId", tvLeft);
                    map1.put("desc", desc);
                    jLpresenter.getRightJl(map1);
                    leftListAdapter.notifyDataSetChanged();
                }
            }
        });
        rightAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(RhxhTypeActivity.this, ShangpinDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("shopGoodsCode", rightList.get(position).getCommodityCode());
                bundle.putString("commodityType", rightList.get(position).getCommodityType());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        leftPresenter.getLeftType(token, msg_type_id);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_rhxh_type;
    }

    @OnClick({R.id.market_back, R.id.market_search, R.id.market_gouwuche_rl, R.id.tv_paiming, R.id.rl_price, R.id.rl_pingfen, R.id.rl_juli})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.market_back://返回
                finish();
                break;
            case R.id.market_search://搜索
                startActivity(new Intent(RhxhTypeActivity.this, SearchActivity.class));
                break;
            case R.id.market_gouwuche_rl://购物车
                Intent intent = new Intent(RhxhTypeActivity.this, MainActivity.class);
                intent.putExtra("type", "购物车");
                startActivity(intent);
                finish();
                break;
            case R.id.tv_paiming://排名
                tvRight = "综合排名";
                desc = 0;
                isFirst0 = 0;
                isFirst1 = 0;
                isFirst2 = 0;
                ivDi.setImageResource(R.mipmap.di);
                ivGao.setImageResource(R.mipmap.gao_1);
                switchTab(tvPaiming);
                rightPresenter.getRightData(map);
                break;
            case R.id.rl_price://价格
                if (isFirst0 == 0) {
                    tvRight = "价格";
                    desc = 0;
                    ivGaoPrice.setImageResource(R.mipmap.gao);
                    ivLowerPrice.setImageResource(R.mipmap.di);
                    isFirst0 = 1;
                } else if (isFirst0 == 1) {
                    tvRight = "价格";
                    desc = 1;
                    ivGaoPrice.setImageResource(R.mipmap.gao_1);
                    ivLowerPrice.setImageResource(R.mipmap.di_1);
                    isFirst0 = 0;
                }
                switchImage(ivGaoPrice, ivLowerPrice);
                switchTab(tvPrice);
                rightPresenter.getRightData(map);
                break;
            case R.id.rl_pingfen://评分
                if (isFirst1 == 0) {
                    tvRight = "评分";
                    desc = 0;
                    ivGoaPingfen.setImageResource(R.mipmap.gao);
                    ivLowerPingfen.setImageResource(R.mipmap.di);
                    isFirst1 = 1;
                } else if (isFirst1 == 1) {
                    tvRight = "评分";
                    desc = 1;
                    ivGoaPingfen.setImageResource(R.mipmap.gao_1);
                    ivLowerPingfen.setImageResource(R.mipmap.di_1);
                    isFirst1 = 0;
                }
                switchImage(ivGoaPingfen, ivLowerPingfen);
                switchTab(tvPingfen);
                rightPresenter.getRightData(map);
                break;
            case R.id.rl_juli://距离
                map1.put("token", token);
                map1.put("typeId", tvLeft);
                if (isFirst2 == 0) {
                    tvRight = "距离远近";
                    desc = 0;
                    ivGaoJuli.setImageResource(R.mipmap.gao);
                    ivLowerJuli.setImageResource(R.mipmap.di);
                    isFirst2 = 1;
                } else if (isFirst2 == 1) {
                    tvRight = "距离远近";
                    desc = 1;
                    ivGaoJuli.setImageResource(R.mipmap.gao_1);
                    ivLowerJuli.setImageResource(R.mipmap.di_1);
                    isFirst2 = 0;
                }
                map1.put("desc", 1);
                map1.put("lat", spfUtils.getLatitude());
                map1.put("lng", spfUtils.getLongitude());
                switchImage(ivGaoJuli, ivLowerJuli);
                switchTab(tvJuli);
                jLpresenter.getRightJl(map1);
                break;
        }
    }

    /**
     * 改变文字颜色
     *
     * @param tv
     */
    private void switchTab(TextView tv) {
        if (tvType != tv) {
            tv.setTextColor(getResources().getColor(R.color.colorEC921A));//当前选中的
            tvType.setTextColor(getResources().getColor(R.color.color454444));//之前选中的
        }
        tvType = tv;
        map.put("sortType", tvRight);
        map.put("typeId", tvLeft);
        map.put("desc", desc);
    }

    /**
     * 改变箭头
     *
     * @param igGao
     * @param igDi
     */
    private void switchImage(ImageView igGao, ImageView igDi) {
        if (ivGao != igGao) {
            ivGao.setImageResource(R.mipmap.gao_1);
            ivGao = igGao;
        }
        if (ivDi != igDi) {
            ivDi.setImageResource(R.mipmap.di);
            ivDi = igDi;
        }
        switch (tvRight) {
            case "价格":
                isFirst1 = 0;
                isFirst2 = 0;
                break;
            case "评分":
                isFirst0 = 0;
                isFirst2 = 0;
                break;
            case "距离远近":
                isFirst0 = 0;
                isFirst1 = 0;
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据访问失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getRightData(RightBean rightBean) {
        if (rightBean.getState() == 0) {
            Toast.makeText(this, "接口有误", Toast.LENGTH_SHORT).show();
        } else if (rightBean.getState() == 1) {
            rightList.clear();
            List<RightBean.DataBean> data = rightBean.getData();
            if (data != null && data.size() > 0) {
                rightList.addAll(data);
                rlvRhxh.setVisibility(View.VISIBLE);
                ivNoData.setVisibility(View.GONE);
                rightAdapter.notifyDataSetChanged();
            }else{
                rlvRhxh.setVisibility(View.GONE);
                ivNoData.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void getLeftType(LeftBean leftBean) {
        if (leftBean.getState() == 0) {
            Toast.makeText(this, "接口访问有误", Toast.LENGTH_SHORT).show();
        } else if (leftBean.getState() == 1) {
            data = leftBean.getData();
            for (int i = 0; i < data.size(); i++) {
                leftList.add(data.get(i).getTypeName());
                if (i == 0) {
                    flagArray.add(i, true);
                } else {
                    flagArray.add(i, false);
                }
            }
            leftListAdapter.notifyDataSetChanged();
            tvLeft = data.get(0).getTypeId();
            map.put("typeId", tvLeft);
            rightPresenter.getRightData(map);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (rightPresenter != null) {
            rightPresenter = null;
        }
        if (leftPresenter != null) {
            leftPresenter = null;
        }
        if (jLpresenter != null) {
            jLpresenter = null;
        }
    }

    @Override
    public void getJuliData(RightBean rightBean) {
        if (rightBean.getState() == 0) {
            Toast.makeText(this, "接口有误", Toast.LENGTH_SHORT).show();
        } else if (rightBean.getState() == 1) {
            rightList.clear();
            List<RightBean.DataBean> data = rightBean.getData();
            if (data != null && data.size() > 0) {
                rightList.addAll(data);
                rlvRhxh.setVisibility(View.VISIBLE);
                ivNoData.setVisibility(View.GONE);
                rightAdapter.notifyDataSetChanged();
            }else{
                rlvRhxh.setVisibility(View.GONE);
                ivNoData.setVisibility(View.VISIBLE);
            }
        }
    }
}
